<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/28 0028
 * Time: 下午 2:27
 */

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use kartik\file\FileInput;
use kartik\select2\Select2;
use yii\web\JsExpression;
use app\common\Functions;


$showForm = []; // 显示表格

//默认的字段类型
$defineFieldStyle = [
    'template'      => "{label}\n<div class=\"col-sm-8\">{input}</div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-3 control-label']];



$defineFieldStyleThree = [
    'template'      => "{label}\n<div class=\"col-sm-7\">{input}</div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-5 control-label']];

$defineFieldStyle3 = [
    'template'      => "{input}\n{error}",
    'labelOptions' => ['style'=>'display:none']];

$defineFieldStyle4 = [
    'template'      => "{label}\n<div class=\"col-sm-8\">{input}</div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-3 control-label']];

$defineFieldStyle5 = [//元
    'template'      => "{label}\n<div class=\"col-sm-8\"><div class=\"input-group\">{input}<span class=\"input-group-addon\">元</span></div></div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-3 control-label']];
$defineFieldStyle6 = [//个
    'template'      => "{label}\n<div class=\"col-sm-8\"><div class=\"input-group\">{input}<span class=\"input-group-addon\">个</span></div></div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-3 control-label']];
$defineFieldStyle7 = [//单元
    'template'      => "{label}\n<div class=\"col-sm-8\"><div class=\"input-group\">{input}<span class=\"input-group-addon\">单元</span></div></div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-3 control-label']];
$defineFieldStyle8 = [//层
    'template'      => "{label}\n<div class=\"col-sm-8\"><div class=\"input-group\">{input}<span class=\"input-group-addon\">层</span></div></div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-3 control-label']];
$defineFieldStyle9 = [//平方
    'template'      => "{label}\n<div class=\"col-sm-8\"><div class=\"input-group\">{input}<span class=\"input-group-addon\">平方</span></div></div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-3 control-label']];
$defineFieldStyle10 = [//元/每月
    'template'      => "{label}\n<div class=\"col-sm-8\"><div class=\"input-group\">{input}<span class=\"input-group-addon\">元/月</span><span id='price' class=\"input-group-addon\">**元/㎡/月</span></div></div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-3 control-label']];

$defineFieldStyle11 = [
    'template'      => "{label}\n<div class=\"col-sm-8\">{input}</div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-3 control-label']];
//
//['labelOptions'=>['style'=>'display:none']]

$ownCustStyles = [];
if ($model->ownCusts )
{
    if ($model->ownCusts->cust_name )
    {
        if ($model->own_cust_id!=0)
        {
            $ownCustStyles['initValueText'] = $model->ownCusts->cust_name;
        }
    }
}
$ownCustStyles['options'] = ['placeholder' => '请选择 ...'];

$ownCustStyles['pluginOptions'] = [
    'placeholder' => '查询 ...',
    'allowClear' => true,
    'minimumInputLength' => 1,
    'language' => 'zh-CN',
//        [
//        'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
//    ],
    'ajax' => [
        'url' => 'seachcust',
        'dataType' => 'json',
        'data' => new JsExpression('function(params) { return {q:params.term}; }')
    ],
    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
    'templateResult' => new JsExpression('function(res) {

            return res.text; }'),
    'templateSelection' => new JsExpression('function (res) {
                    return res.text; }')];

// 区域是否隐藏
$regionStyle='display:none';
if ($update)
{
    $regionStyle ='display:show';
}

$useCustStyles = [];
if ($model->useCusts->cust_name)
{
    $useCustStyles['initValueText'] = $model->useCusts->cust_name;
}

$useCustStyles['options'] = ['placeholder' => '请选择 ...'];

$useCustStyles['pluginOptions'] = [
    'placeholder' => '查询 ...',
    'allowClear' => true,
    'minimumInputLength' => 1,
    'language' => 'zh-CN',

//        [
//        'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
//    ],
    'ajax' => [
        'url' => 'seachcust',
        'dataType' => 'json',
        'data' => new JsExpression('function(params) { return {q:params.term}; }')
    ],
    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
    'templateResult' => new JsExpression('function(res) {

            return res.text; }'),
    'templateSelection' => new JsExpression('function (res) {
                    return res.text; }')];

// 初始化员工类型样式
$orderStaffIDStyles = [];
$orderStaffIDStyles['options'] = ['placeholder' => '请选择 ...'];

$orderStaffIDStyles['pluginOptions'] = [
    'placeholder' => '查询 ...',
    'allowClear' => true,
    'minimumInputLength' => 1,
    'language' =>  'zh-CN',

//        [
//        'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
//    ],
    'ajax' => [
        'url' => 'seachorderstaff',
        'dataType' => 'json',
        'data' => new JsExpression('function(params) { return {q:params.term}; }')
    ],
    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
    'templateResult' => new JsExpression('function(res) {

            return res.text; }'),



    'templateSelection' => new JsExpression('function (res) {

                    if (res.id!=res.text)
                    {
                        $("input#prodinst-orderstaffid").val(res.id);
                    }


                    return res.text; }')];


?>

<?php $form = ActiveForm::begin([
    'options'=>['class' => 'form-horizontal','enctype'=>'multipart/form-data']
]); ?>

<?php
// 构建展示膜版的数组
$showForm = [];


// 查找出订单的特性10002
foreach ($attrSpecs as $attrSpec)
{
    $prodAttr = $attrSpec->attrSpecs;

    if($prodAttr->attr_spec_id==41){
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle10 )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]);
    }elseif($prodAttr->attr_spec_id==4){
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle7 )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]);
    }elseif($prodAttr->attr_spec_id==14){
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle8 )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]);
    }elseif($prodAttr->attr_spec_id==8){
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle9 )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]);
    }elseif($prodAttr->attr_spec_id==5  ){
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle )->label($prodAttr->attr_spec_name)->textarea(['maxlength' => 164]);
    }elseif( $prodAttr->attr_spec_id==49  || $prodAttr->attr_spec_id==52  || $prodAttr->attr_spec_id==25 ){
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model,"localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]" , $defineFieldStyle4 )->label($prodAttr->attr_spec_name)->dropDownList(Functions::getDomains('attr_spec',$prodAttr->attr_spec_id));
    }elseif($prodAttr->attr_spec_id==80){
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle5 )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]);
    }elseif($prodAttr->attr_spec_id==79 || $prodAttr->attr_spec_id==47 ){
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle4 )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]);
    }elseif( $prodAttr->attr_spec_id==50){
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle11 )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64 ,'value'=>'押二付一']);
    }else{
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle6 )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]);
    }
//Yii::trace("==================++++++++++++++++++++++++++");
//Yii::trace($showForm);

}


//照片
if ($prodAttr->attr_spec_type_id==2){
    if( $prodAttr->attr_spec_id==6){
        $pluginOptions=[];
        $pluginOptions['initialPreviewAsData'] = true;
        $pluginOptions['showRemove'] = true;
        $pluginOptions['showUpload'] = false;

            if ($p1)
            {

                foreach ($p1[$prodAttr->attr_spec_id] as $imageFile)
                {
                    $pluginOptions['initialPreview'][] = $imageFile;
                }
               // $pluginOptions['initialPreview'] = $p1[$prodAttr->attr_spec_id];
            }

            //localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]
        $showForm[$prodAttr->attr_spec_id]['form'] = $form->field($uploadModel, "file[]",$defineFieldStyle)->label($prodAttr->attr_spec_name)->widget(FileInput::classname(), [
            'options' => ['multiple' => true, 'accept' => 'image/*'],
            'pluginOptions' =>$pluginOptions,
        ]);//->fileInput();
        //$showForm[$prodAttr->attr_spec_id]['form'] = $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle11 )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64 ,'value'=>'押二付一']);
    }
}


?>

<?php
// 构建展示膜版的数组
$showForms = [];

$showForms['cust_name']['form']       = $form->field($model2, 'cust_name' , $defineFieldStyle)->label( '房东姓名')->textInput(['maxlength' => 64]);

$showForms['tel']['form']  = $form->field($model2, 'tel' ,$defineFieldStyle)->label('联系方式')->textInput(['maxlength' => 64]);


//Yii::trace("==================+++++++++++++++++++++++++");
//Yii::trace($showForms);

$orderStaffIDStyles = [];

if ($update && $model->staffName)
{
    $orderStaffIDStyles['initValueText']  = $model->staffName;
}
$orderStaffIDStyles['options'] = ['placeholder' => '请选择 ...'];
$orderStaffIDStyles['pluginOptions'] = [
    'placeholder' => '查询 ...',
    'allowClear' => true,
    'minimumInputLength' => 1,
    'language' => [
        'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
    ],
    'ajax' => [
        'url' => '/prod/prodinst/seachorderstaff',
        'dataType' => 'json',
        'data' => new JsExpression('function(params) { return {q:params.term}; }')
    ],
    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
    'templateResult' => new JsExpression('function(res) {
                    return res.text; }'),
    'templateSelection' => new JsExpression('function (res) {
                            return res.text; }')];
$showForm['orderStaffID']['form']   =  $form->field($model, 'staff_id',$defineFieldStyle)->label( '分配所有人')->widget(Select2::classname(), $orderStaffIDStyles);


?>


<div class="box-body">

        <div style="display:none">
        <?php
            echo  $form->field($model, 'id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $model->id]);

        ?>
        <input type="text" name="pd" value="666" style="display:none;"/>
        </div>


        <div class="col-md-12" id="block">
            <span class="col-md-12" >
---------------------------------------------房源信息（点击收起）-----------------------------------------
            </span>
        </div>

        <div>
            <div class="col-md-12">

                <?php
                     if ($prod_spec_id==10002 ||  $prod_spec_id==10004 || $prod_spec_id==10001 )
                {
                    $parentStyles=[];

                    if ($model->parentProdName)
                    {

                        $parentStyles['initValueText'] = $model->parentProdName;

                    }

                    $parentStyles['options'] = ['placeholder' => '请选择 ...'];

                    $parentStyles['pluginOptions'] = [
                    'placeholder' => '查询 ...',
                    'allowClear' => true,
                    'minimumInputLength' => 2,
                    'language' => 'zh-CN',

        //                [
        //                'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
        //            ],
                    'ajax' => [
                        'url' => 'seachprodinst',
                        'dataType' => 'json',
                        'data' => new JsExpression('function(params) { return {q:params.term,parent_prod_spec_id: ' . $parent_prod_spec_id . '}; }')
                    ],
                    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                    'templateResult' => new JsExpression('function(res) {

                    return res.text; }'),



                    'templateSelection' => new JsExpression('function (res) {

                            if (res.id!=res.text)
                            {
                                $("input#prodinst-parent_id").val(res.id);
                            }


                            return res.text; }')];

                 echo $form->field($model, 'parent_id',$defineFieldStyle)->label(  '大楼')->widget(Select2::classname(), $parentStyles);
                }

                ?>


            </div>

            <div class="col-md-12">
                <?php  echo   $showForm[4]['form'] ; ?>
            </div>

            <div class="col-md-12">
                <?php  echo   $showForm[14]['form'] ; ?>
            </div>

            <div class="col-md-12">
                <?php  echo   $showForm[8]['form'] ; ?>
            </div>

            <div class="col-md-12">
                <?php  echo   $showForm[47]['form'] ; ?>
                <?php  echo   $showForm[25]['form'] ; ?>
                <?php  echo   $showForm[49]['form'] ; ?>
            </div>

<!--            <div class="col-md-12">-->
<!--                --><?php // echo   $showForm[25]['form'] ; ?>
<!--            </div>-->
<!---->
<!--            <div class="col-md-12">-->
<!--                --><?php // echo   $showForm[49]['form'] ; ?>
<!--            </div>-->




            <div class="col-md-12">
                <?php  echo   $showForm[53]['form'] ; ?>
            </div>

            <div class="col-md-12">
                <?php  echo   $showForm[79]['form'] ; ?>
            </div>


            <div class="col-md-12">
                <?php  echo   $showForms['tel']['form'] ; ?>
            </div>

            <div class="col-md-12">
                <?php  echo   $showForms['cust_name']['form'] ; ?>
            </div>

            <div class="col-md-12">
                <?php  echo   $showForm[5]['form'] ; ?>
            </div>


        </div>



        <div class="col-md-12" id="housing">
            <span class="col-md-12">
---------------------------------------------租赁要求（点击收起）-----------------------------------------
            </span>

        </div>
        <div>

            <div class="col-md-12">
                <?php  echo   $showForm[41]['form'] ; ?>

            </div>

            <div class="col-md-12">
                <?php  echo   $showForm[80]['form'] ; ?>
            </div>

            <div class="col-md-12">
                <?php  echo   $showForm[50]['form'] ; ?>
            </div>



            <div class="col-md-12">
                <?php  echo   $showForm[52]['form'] ; ?>
            </div>

            <div style="display:none">
                <?php echo $form->field($model, "localProdAttrsInsts[6][attrSpecTypeID]",['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $prodAttr->attr_spec_type_id]); ?>
            </div>

            <div class="col-md-12">
                <?php  echo   $showForm[6]['form'] ; ?>
            </div>


            <div class="col-md-12">
                <?php  echo   $showForm['orderStaffID']['form'] ; ?>


            </div>

        </div>





        <div class="box-footer">
            <?php echo Html::submitButton($model->getIsNewRecord() ? Yii::t('region', 'Create') : Yii::t('region', 'Update'), [
    'class' => $model->getIsNewRecord() ? 'btn btn-success' : 'btn btn-primary', ]); ?>
            <?php echo Html::a(Yii::t('region', 'Cancel'), Yii::$app->request->getReferrer(), ['class' => 'btn btn-default']); ?>
        </div>

    </div>
    <?php ActiveForm::end(); ?>

</div>







<?php
$js = <<<JS
jQuery('#block').on('click',function() {

    $(this).next().toggle();


});
JS;
$this->registerJs($js);

$js = <<<JS
jQuery('#housing').on('click',function() {

    $(this).next().toggle();


});
JS;
$this->registerJs($js);


$js = <<<JS
jQuery('#prodinst-localprodattrsinsts-41-content').on('blur',function() {

   $("#price").html( ($(this).val()/$("#prodinst-localprodattrsinsts-8-content").val()).toFixed(1)+'元/㎡/月');

});
JS;
$this->registerJs($js);

$js = <<<JS

function checkPhone(phone){

    if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
        //alert("手机号码有误，请重填");
        return false;
    }
    return true;
}

jQuery('input#cust-tel').on('blur',function() {

    // 如果没填值 则直接返回错误
    if ($(this).val()== "")
    {
        $(this).parent().parent().addClass('has-error');
        $(this).parent().siblings(".help-block").text('号码不为空！');
        return false;
    }
    else
    {
        if ( !checkPhone($(this).val()))
         {
            $(this).parent().parent().addClass('has-error');
            $(this).parent().siblings(".help-block").text('请填写正确的号码！');
             return false;

         }
         $(this).parent().parent().removeClass("has-error");
         $(this).parent().siblings(".help-block").text('');
         $.get(
        '/prod/order/repeatorder',
        {
            tel: $(this).val()
        },
        function (data) {
            $("#cust-cust_name").val(data.results.cust_name);

        });
    }
});

jQuery('input#prodinst-localprodattrsinsts-4-content').on('blur',function() {//门牌号

    // 如果没填值 则直接返回错误
    if ($(this).val()== "")
    {
        $(this).parent().parent().parent().addClass('has-error');
        $(this).parent().parent().siblings(".help-block").text('必填！');
        return false;
    }
});

jQuery('input#prodinst-localprodattrsinsts-4-content').on('blur',function() {

    $.get(
        '/prod/prodinst/repeat',
        {
            parent_id: $("#parent_id").val(),
            room_id: $(this).val() 
        },
        function (data) {
        
           if (data.results.code>0)
           {
                $(this).parent().addClass('has-error');
                $(this).siblings(".help-block").text('房间号重复！');
                 return false;
           }
           $(this).parent().removeClass("has-error");
           $(this).siblings(".help-block").text('');
           return ture;
         
           
        }  
    );
});

jQuery('input#prodinst-localprodattrsinsts-14-content').on('blur',function() {//楼层

    if ($(this).val()== "")
    {
        $(this).parent().parent().parent().addClass('has-error');
        $(this).parent().parent().siblings(".help-block").text('必填！');
        return false;
    }
});
jQuery('input#prodinst-localprodattrsinsts-8-content').on('blur',function() {//建筑面积

    if ($(this).val()== "")
    {
        $(this).parent().parent().parent().addClass('has-error');
        $(this).parent().parent().siblings(".help-block").text('必填！');
        return false;
    }
})


JS;
$this->registerJs($js);

$js = <<<JS
var checkNumber = function( ){

    var c=$(this);
    if(/[^\d]/.test(c.val())){//替换非数字字符
        var temp_amount=c.val().replace(/[^\d]/g,'');
              $(this).val(temp_amount);
            }
}
$('input#prodinst-localprodattrsinsts-14-content').keyup(checkNumber);//楼层的数字替换
JS;
$this->registerJs($js);





?>